package com.orange.base.component.biztool.simulateddata.service.impl;

import java.util.Calendar;

import com.orange.base.component.biztool.simulateddata.service.SimulatedDataEnum;
import com.orange.base.component.biztool.simulateddata.service.SimulatedDataParam;

/**
 * yyyy-MM-dd HH:mm
 *
 * @author biankudingcha
 * @date: 2022-08-24
 * @since 0.5.0
 */
public class DateTimeStrategy extends AbstractSimulatedData {

    public String getType() {
        return SimulatedDataEnum.DATE_TIME.getName();
    }

    public String getCaption() {
        return SimulatedDataEnum.DATE_TIME.getCaption();
    }

    @Override
    public Object getValue(SimulatedDataParam param) {
        return generate(param);
    }

    public static String generate(SimulatedDataParam param) {
        String min = param.getMin();
        String max = param.getMax();
        Integer start = null;
        Integer end = null;
        if (!isEmpty(min) && min.length() > 4) {
            start = toInt(min.substring(0, 4));
        }
        if (!isEmpty(max) && max.length() > 4) {
            end = toInt(max.substring(0, 4));
        }
        return getYear(start, end) + "-" + getMonth() + "-" + getDay() + " " + getHour() + ":" + getMinute();
    }

    public static String getYear(Integer start, Integer end) {
        if (start == null) {
            start = 1900;
        }
        if (end == null) {
            Calendar calendar = Calendar.getInstance();
            end = calendar.get(Calendar.YEAR);
        }
        return getRandomNumber(start, end) + "";
    }

    public static String getMonth() {
        int month = getRandomNumber(1, 12);
        if (month < 10) {
            return "0" + month;
        }
        return month + "";
    }

    public static String getDay() {
        int day = getRandomNumber(1, 28);
        if (day < 10) {
            return "0" + day;
        }
        return day + "";
    }

    public static String getHour() {
        int hour = getRandomNumber(0, 23);
        if (hour < 10) {
            return "0" + hour;
        }
        return hour + "";
    }

    public static String getMinute() {
        int minute = getRandomNumber(0, 59);
        if (minute < 10) {
            return "0" + minute;
        }
        return minute + "";
    }

    public static String getSecond() {
        int second = getRandomNumber(0, 59);
        if (second < 10) {
            return "0" + second;
        }
        return second + "";
    }

}
